using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.DocumentFormat.OpenXml.Packaging;
using System.IO;

namespace OpenXMLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateNewWordDocument( @"C:\Temp\Test.docx");

            Console.ReadLine();
        }

        // How to: Create a new package as a Word document.
        public static void CreateNewWordDocument(string document)
        {
            using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
            {
                // Set the content of the document so that Word can open it.
                MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();

                SetMainDocumentContent(mainPart);
            }
        }

        // Set content of MainDocumentPart.
        public static void SetMainDocumentContent(MainDocumentPart part)
        {
            const string docXml =
         @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> 
            <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
                <w:body>
                    <w:p>
                        <w:r>
                            <w:t>Hello world!</w:t>
                        </w:r>
                     </w:p>
                 </w:body>
            </w:document>";

            using (Stream stream = part.GetStream())
            {
                byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
                stream.Write(buf, 0, buf.Length);
            }

            string fileName = @"C:\temp\sdk.jpg";
            ImagePart imagePart = part.AddImagePart(ImagePartType.Jpeg);

            using (FileStream stream = new FileStream(fileName, FileMode.Open))
            {
                imagePart.FeedData(stream);
            }
        }
    }
}
